package edu.scau.hibernate.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import edu.scau.hibernate.model.User;
import edu.scau.hibernate.util.*;


public class UserDaoImpl implements UserDao{
	
	private Session session;
	public UserDaoImpl(){
		session=HibernateUtil.getSession();
	}
	
		@Override
		public boolean save(User user) throws Exception {
	          session.save(user);
			  return true;    
		}

		@Override
		public boolean delete(User user) throws Exception {
			  session.delete(user);
			  return true;
				
		}
		@Override
		public boolean update(User user) throws Exception {
				session.update(user);
			   return true;
		}
		
		@Override
		public User findById(int userId) throws Exception {
			User user=(User)session.get(User.class, userId);
			return user;
		}
		
		@Override
		public User findByUserName(String userName) throws Exception {
			User user = null;
			String hql="from User user where userName='"+userName+"'";
			Query query=session.createQuery(hql);
			List list=query.list();
			if(list.size()==1)
			    user=(User) list.get(0);
			return user;
		}
		@Override
		public List<User> findAll() throws Exception {
			String hql="from User user";
			Query query=session.createQuery(hql);
			List list=query.list();
			return list;
		}
		
}
